package Package.Test4;

import java.util.Comparator;
import java.util.TreeSet;

public class Test4 {
    public static final void Test4(){
//        案例需求
//用TreeSet集合存储多个学生信息(姓名，语文成绩，数学成绩)，并遍历该集合
//要求：按照总分从高到低出现
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int Total_score1=o1.getMath()+o1.getChinese();
                int Total_score2=o2.getMath()+o2.getChinese();
                int num=Total_score1-Total_score2;
                num=num==0?o1.getName().compareTo(o2.getName()):num;
                return num;
            }
        });
        //创建学生对象
        Student s1 = new Student("林青霞",20, 98, 100);
        Student s2 = new Student("张曼玉", 20,95, 95);
        Student s3 = new Student("王祖贤", 20,100, 93);
        Student s4 = new Student("柳岩", 20,100, 97);
        Student s5 = new Student("风清扬",20, 98, 98);
        Student s6 = new Student("左冷禅",20, 97, 99);
// Student s7 = new Student("左冷禅", 97, 99);
        Student s7 = new Student("赵云",20, 97, 99);
//把学生对象添加到集合
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);
//遍历集合
        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getChinese() + "," +
                    s.getMath() + "," + s.getSum());
        }
    }
}
